/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *  SRVStateBlackfinAvailable.m - iPhoneSRV console for Surveyor SRV-1 and SVS 
 *
 *  Created by Nick Kitchener.  Copyright (C) 2009  Surveyor Corporation 
 *            and Nick Kitchener.  All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details (www.gnu.org/licenses)
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#import "SRVStateBlackfinAvailable.h"
#import "SRVStateInitialised.h"
#import "SRVInterthreadMessageStreamable.h"

@implementation SRVStateBlackfinAvailable

-(void) stateInitialise {
	[[stateSwitcher connectionStateListener] blackfinAvailable:stateSwitcher];
}

- (void) disconnect {
	// need to initate a disconnect
	[stateSwitcher setState:[SRVStateInitialised alloc]];
}

- (void) setOperation:(SRVOperation *)operation {
	currentOperation = operation;
	SRVInterthreadMessageStreamable *streamable = [[SRVInterthreadMessageStreamable alloc] init];
	[streamable setObject:operation];
	[stateSwitcher sendMessage:streamable];
}

- (void)handleChildMessage:(SRVInterthreadMessage *)msg {
	if( msg == nil ) {
		NSLog(@"received nil message!");
	}
	switch( [msg type] ) {
		case DISCONNECT: // disconnected
			NSLog(@"SRVBlackfinController User disconection completed response received.");
			[self disconnectComplete:self];
			break;
		case STREAMABLE: {
			//NSLog(@"SRVBlackfinController streamable response received.");
			SRVInterthreadMessageStreamable *s = (SRVInterthreadMessageStreamable*) msg;
			[s release];
			[currentOperation setOperationComplete:YES]; // operation should do this itself..
			[[currentOperation completionListener] operationCompleted:currentOperation];
		}
			break;
		default:
			NSLog(@"SRVBlackfinController received unknown interthread message @", msg);
			break;
	}
	
}

@end
